<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改商品管理')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-product-edit" th:object="${product}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">商品名称：</label>
                <div class="col-sm-8">
                    <input name="name" th:field="*{name}" class="form-control" type="text" disabled>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">商品单价：</label>
                <div class="col-sm-8">
                    <input name="price" th:field="*{price}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">开始时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="startTime" th:value="${#dates.format(product.startTime, 'yyyy-MM-dd HH:mm:ss')}" class="form-control" placeholder="yyyy-MM-dd HH:mm:ss" type="text" required>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">结束时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="endTime" th:value="${#dates.format(product.endTime, 'yyyy-MM-dd HH:mm:ss')}" class="form-control" placeholder="yyyy-MM-dd HH:mm:ss" type="text" required>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">库存数量：</label>
                <div class="col-sm-8">
                    <input name="count" th:field="*{count}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">商品描述：</label>
                <div class="col-sm-8">
                    <textarea name="describe" class="form-control" required>[[*{describe}]]</textarea >
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">商品图片：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="img" th:field="*{img}">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="img" name="file" type="file">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <script th:inline="javascript">
        var prefix = ctx + "system/product";
        $("#form-product-edit").validate({
            focusCleanup: true,
            rules: {
                price:{
                    required: true,
                    number: true,

                },
                startTime: {
                    required: true,
                },
                endTime: {
                    required: true,
                },
                count:{
                    required:true,
                    digits:true,
                }
            },
            messages:{
                price: {
                    required: "请输入商品单价"
                },
                count: {
                    required: "请输入商品库存"
                },
                startTime: {
                    required: "请输入开始时间"
                },
                endTime: {
                    required: "请输入结束时间"
                },
                describe: {
                    required: "请输入商品描述"
                }
            }
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-product-edit').serialize());
            }
        }

        $("input[name='startTime']").datetimepicker({
            format: "yyyy-mm-dd hh:ii:00", //选择日期后，文本框显示的日期格式
            language: 'zh-CN', //汉化
            minView: 'hour',
            minuteStep:1,
            autoclose:true //选择日期后自动关闭
        });

        $("input[name='endTime']").datetimepicker({
            format: "yyyy-mm-dd hh:ii:00", //选择日期后，文本框显示的日期格式
            language: 'zh-CN', //汉化
            minView: 'hour',
            minuteStep:1,
            autoclose:true //选择日期后自动关闭
        });

        $(".file-upload").each(function (i) {
            var val = $("input[name='" + this.id + "']").val()
            $(this).fileinput({
                'uploadUrl': ctx + 'common/upload',
                initialPreviewAsData: true,
                initialPreview: [val],
                maxFileCount: 1,
                autoReplace: true
            }).on('fileuploaded', function (event, data, previewId, index) {
                $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
            }).on('fileremoved', function (event, id, index) {
                $("input[name='" + event.currentTarget.id + "']").val('')
            })
            $(this).fileinput('_initFileActions');
        });


        $.validator.addMethod("digits",function(value,element,params){
            var digits= /^[1-9]\d*$/;
            return this.optional(element)||(digits.test(value));
        },"请输入正整数！");

        $.validator.addMethod("number",function(value,element,params){
            var number= /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
            return this.optional(element)||(number.test(value));
        },"请输入规范的商品价格！");

    </script>
</body>
</html>